#include <QDomDocument>
#include <QFile>
#include <QTextStream>

#include "ConnectionSettings.h"


bool connectionSettingsToXML( const QString& file_name, const ConnectionSettings& settings)
{
    QDomDocument doc;
    QDomElement root = doc.createElement("settings");
    doc.appendChild(root);

    QDomElement version = doc.createElement("Version");
    QDomText textXmlNode = doc.createTextNode( "1.0" );
    version.appendChild( textXmlNode );
    root.appendChild(version);

    QDomElement connect = doc.createElement("connect");
    connect.setAttribute( "useProxy", settings.useProxy ? "1" : "0" );
    connect.setAttribute( "proxy_addres", settings.proxy_addres );
    connect.setAttribute( "proxy_port", QString::number( settings.proxy_port ) );
    connect.setAttribute( "user", settings.user );
    connect.setAttribute( "pass", settings.pass );
    root.appendChild(connect);

    QFile xmlFile(file_name);
    if (!xmlFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
        return false;

    QTextStream stream( &xmlFile );
    doc.save( stream, 4 );
    stream.flush();

    xmlFile.close();
    return true;
}
